/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hochschild.sgv.struts.action;

import com.hochschild.sgv.domain.Reporte;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author LENOVO
 */
public class ImprimirReportePlanificadas extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //response.setContentType("text/html;charset=UTF-8");
        //PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession();
            List<Reporte> lstReportes = new ArrayList<Reporte>();
            lstReportes = (ArrayList<Reporte>)session.getAttribute("lstReporteVacacionesPlanificadas");
            System.out.println("lstReportes.size(): |" + lstReportes.size() + "|");
            //session.removeAttribute("lstReporteVacacionesPlanificadas");
            Document document = new Document(PageSize.A4.rotate());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            document.open();
            document.add(createTituloYSubtitulo("GESTIÓN DE VACACIONES", "REPORTE VACACIONES PLANIFICADAS"));
            document.add(createDetalleReporteVacacionesPlanificadas(lstReportes));

            document.close();
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control",
                    "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            response.setContentType("application/pdf");
            response.setContentLength(baos.size());
            ServletOutputStream os = response.getOutputStream();
            baos.writeTo(os);
            os.flush();
            os.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    public PdfPTable createTituloYSubtitulo(String titulo, String dtTitulo) throws IOException, DocumentException {
        PdfPTable table = new PdfPTable(2);
        table.setWidths(new int[]{5, 5});
        table.setWidthPercentage(100);
        PdfPCell cell;
        Font font = new Font(CreateFont(), 10, Font.NORMAL, BaseColor.WHITE);
        cell = new PdfPCell(new Phrase(String.format(
                "%s", titulo), font));
        cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        cell.setBackgroundColor(new BaseColor(0, 0, 0));
        cell.setColspan(2);
        cell.setFixedHeight(32f);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setBackgroundColor(new BaseColor(178, 178, 176));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setColspan(2);
        cell.setFixedHeight(2f);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(""));
        cell.setBackgroundColor(new BaseColor(249, 184, 20));
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setColspan(2);
        cell.setFixedHeight(2f);
        table.addCell(cell);

        font = new Font(CreateFont(), 8, Font.NORMAL, BaseColor.WHITE);
        cell = new PdfPCell(new Phrase(String.format("%s", dtTitulo), font));
        cell.setBackgroundColor(new BaseColor(0, 0, 0));
        cell.setColspan(4);
        cell.setFixedHeight(16f);
        table.addCell(cell);

        return table;
    }

    public BaseFont CreateFont() {
        BaseFont arial = null;
        String fs = File.separator;
        String ruta = getServletContext().getRealPath(fs);

        try {
            arial = BaseFont.createFont(ruta + "fuente" + fs + "arial.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arial;
    }

    public PdfPTable createDetalleReporteVacacionesPlanificadas(List<Reporte> lstReportes) throws DocumentException {
        PdfPTable table = new PdfPTable(8);
        table.setWidths(new int[]{8, 8, 8, 8, 8, 4, 4, 3});
        table.setWidthPercentage(100);
        BaseColor colorFondoFila;
        PdfPCell cell;

        Font fontcabeceraTabla = new Font(CreateFont(), 7, Font.BOLD, BaseColor.BLACK);

        cell = new PdfPCell(new Phrase("", fontcabeceraTabla));
        cell.setColspan(8);
        cell.setFixedHeight(10f);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("", fontcabeceraTabla));
        cell.setColspan(8);
        cell.setFixedHeight(10f);
        cell.setBackgroundColor(new BaseColor(214, 227, 242));
        cell.setBorderColor(new BaseColor(227, 227, 227));
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Nombre Colaborador", fontcabeceraTabla));
        cell.setBackgroundColor(new BaseColor(249, 249, 249));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorderColor(new BaseColor(227, 227, 227));
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Posición", fontcabeceraTabla));
        cell.setBackgroundColor(new BaseColor(249, 249, 249));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorderColor(new BaseColor(227, 227, 227));
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Nombre Aprobador", fontcabeceraTabla));
        cell.setBackgroundColor(new BaseColor(249, 249, 249));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorderColor(new BaseColor(227, 227, 227));
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Posición", fontcabeceraTabla));
        cell.setBackgroundColor(new BaseColor(249, 249, 249));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorderColor(new BaseColor(227, 227, 227));
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Área", fontcabeceraTabla));
        cell.setBackgroundColor(new BaseColor(249, 249, 249));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorderColor(new BaseColor(227, 227, 227));
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Fec. Inicio", fontcabeceraTabla));
        cell.setBackgroundColor(new BaseColor(249, 249, 249));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorderColor(new BaseColor(227, 227, 227));
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("Fec. Fin", fontcabeceraTabla));
        cell.setBackgroundColor(new BaseColor(249, 249, 249));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorderColor(new BaseColor(227, 227, 227));
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("N° Días", fontcabeceraTabla));
        cell.setBackgroundColor(new BaseColor(249, 249, 249));
        cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        cell.setBorderColor(new BaseColor(227, 227, 227));
        table.addCell(cell);

        //CREACION DEL DETALLE DEL REPORTE DE STATUS
        Font fontDetalleTabla = new Font(CreateFont(), 6, Font.NORMAL, BaseColor.BLACK);
        int i = 0;
        
        for (Reporte reporte : lstReportes) {
            if(i%2 != 0){
                colorFondoFila = new BaseColor(249, 249, 249);
            }
            else{
                colorFondoFila = new BaseColor(255, 255, 255);
            }

            cell = new PdfPCell(new Phrase(reporte.getNombreColaborador(), fontDetalleTabla));
            cell.setBackgroundColor(colorFondoFila);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorderColor(new BaseColor(227, 227, 227));
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(reporte.getPuestoColaborador(), fontDetalleTabla));
            cell.setBackgroundColor(colorFondoFila);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorderColor(new BaseColor(227, 227, 227));
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(reporte.getNombreAprobador(), fontDetalleTabla));
            cell.setBackgroundColor(colorFondoFila);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorderColor(new BaseColor(227, 227, 227));
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(reporte.getPuestoAprobador(), fontDetalleTabla));
            cell.setBackgroundColor(colorFondoFila);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorderColor(new BaseColor(227, 227, 227));
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(reporte.getAreaColaborador(), fontDetalleTabla));
            cell.setBackgroundColor(colorFondoFila);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorderColor(new BaseColor(227, 227, 227));
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(reporte.getFechaInicio(), fontDetalleTabla));
            cell.setBackgroundColor(colorFondoFila);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorderColor(new BaseColor(227, 227, 227));
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(reporte.getFechaFin(), fontDetalleTabla));
            cell.setBackgroundColor(colorFondoFila);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorderColor(new BaseColor(227, 227, 227));
            table.addCell(cell);

            cell = new PdfPCell(new Phrase(reporte.getNumeroDias(), fontDetalleTabla));
            cell.setBackgroundColor(colorFondoFila);
            cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell.setBorderColor(new BaseColor(227, 227, 227));
            table.addCell(cell);

            i++;
        }

        return table;
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
